package com.qeegoo.b2bautozityp.moudle.good.viewmodel.category;

import com.kelin.mvvmlight.command.ReplyCommand;
import com.qeegoo.b2bautozityp.BR;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.api.HttpParams;
import com.qeegoo.b2bautozityp.api.ProgressSubscriber;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.moudle.base.BaseAdapter;
import com.qeegoo.b2bautozityp.moudle.good.model.GoodsCategoryBean;

import java.util.ArrayList;

import base.lib.util.ActivityManager;

/**
 * 王少岩 在 2017/9/19 创建了它
 */

public class GoodsCategorySearchViewModel {
    private RequestApi mRequestApi;
    private BaseAdapter<GoodsCategoryKeyWordViewModel> mAdapter;
    private ArrayList<GoodsCategoryKeyWordViewModel> mKeyWordViewModels = new ArrayList<>();

    public GoodsCategorySearchViewModel(RequestApi requestApi) {
        mRequestApi = requestApi;
        mAdapter = new BaseAdapter<>(BR.viewModel, R.layout.adapter_goods_category_keyword, mKeyWordViewModels);
        getCategorysBykeyWords("");
    }


    public BaseAdapter<GoodsCategoryKeyWordViewModel> getAdapter() {
        return mAdapter;
    }

    public void getCategorysBykeyWords(String keyWords) {
        mRequestApi.listProductBykeyWords(HttpParams.goodsCategoryBykeyWords(keyWords))
                .compose(RetrofitService.applySchedulers())
                .map(new RetrofitService.HttpResultFunc<>())
                .subscribe(new ProgressSubscriber<ArrayList<GoodsCategoryBean>>() {
                    @Override
                    public void onNext(ArrayList<GoodsCategoryBean> goodsCategorys) {
                        mKeyWordViewModels.clear();
                        for (GoodsCategoryBean b : goodsCategorys) {
                            mKeyWordViewModels.add(new GoodsCategoryKeyWordViewModel(b, keyWords));
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }

    public ReplyCommand leftCommand = new ReplyCommand(() -> ActivityManager.getActivity().finish());
    public ReplyCommand<String> afterCommand = new ReplyCommand<>(this::getCategorysBykeyWords);
}
